package com.fansl.allround.upms.controller.inner;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fansl.allround.common.core.bean.UserInfo;
import com.fansl.allround.common.core.constant.enums.OauthTypeEnum;
import com.fansl.allround.common.core.exception.AllroundBizException;
import com.fansl.allround.common.security.annotation.Inner;
import com.fansl.allround.upms.api.dto.BindSocialInfoDTO;
import com.fansl.allround.upms.api.dto.SysSocialUserDTO;
import com.fansl.allround.upms.api.dto.UnbindSocialInfoDTO;
import com.fansl.allround.upms.api.entity.SysUser;
import com.fansl.allround.upms.service.SysUserService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

/**
 * @author fansl
 * @Description: 用户控制器 内部使用
 * @date 2019/11/6 11:20
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/inner/user")
public class UserInnerController {
    private final SysUserService userService;

    /**
     * 通过社交账号查询用户、角色信息
     *
     * @param openid
     * @param oauthType 认证类型
     * @return
     */
    @Inner
    @GetMapping("/social/info/{openid}")
    public UserInfo social(@PathVariable("openid") String openid,
                           @RequestParam("oauthType") OauthTypeEnum oauthType) {
        switch (oauthType) {
            case WEIBO:
                break;
            case WECHAT:
                break;
            case QQ:
                break;
            default:
                throw new AllroundBizException("oauth type not support");
        }
        SysUser user = userService.getByOauthType(oauthType, openid);
        return Optional.ofNullable(user)
                .map((sysUser) -> {
                    log.info("获取到社交绑定的用户ID：{}，查找用户信息", sysUser.getUserId());
                    return userService.getUserInfo(user);
                }).orElse(new UserInfo());
    }

    /**
     * 内部注册社交用户
     *
     * @param socialUser
     * @return
     */
    @Inner
    @PostMapping("/social/create")
    public UserInfo createSocialUser(@RequestBody SysSocialUserDTO socialUser) {
        return userService.createSocialUser(socialUser);
    }

    /**
     * 绑定第三方应用
     *
     * @param bindInfo 三方应用信息
     * @return true/false
     */
    @Inner
    @PostMapping("/social/bind")
    public Boolean bindSocialInfo(@RequestBody BindSocialInfoDTO bindInfo) {
        return userService.bindSocialInfo(bindInfo);
    }

    /**
     * 绑定第三方应用
     *
     * @param unbindInfo 三方应用信息
     * @return true/false
     */
    @Inner
    @PostMapping("/social/unbind")
    public Boolean bindSocialInfo(@RequestBody UnbindSocialInfoDTO unbindInfo) {
        return userService.unbindSocialInfo(unbindInfo);
    }

    /**
     * 获取指定用户全部信息
     *
     * @return 用户信息
     */
    @Inner
    @GetMapping("/mobile/{mobile}")
    public UserInfo mobile(@PathVariable String mobile) {
        SysUser user = userService.getOne(Wrappers.<SysUser>query()
                .lambda().eq(SysUser::getPhone, mobile));
        return userService.getUserInfo(user);
    }
}
